//
//  FilterPriceViewController.m
//  Fangmm
//
//  Created by 阮琦 on 16/3/1.
//  Copyright © 2016年 com.fangmm. All rights reserved.
//

#import "FilterPriceViewController.h"
#import "NMRangeSlider.h"
#import "ACMacros.h"
#import <MobClick.h>

@interface FilterPriceViewController () <UITableViewDataSource, UITableViewDelegate>

@end

@implementation FilterPriceViewController {
    NMRangeSlider *rangeSlider;
    NSInteger selectedRow;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self insertBackground];
    [self configureTableViews];
    [self configureSliders];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
            [MobClick beginLogPageView:@"价格筛选页"];//("PageOne"为页面名称，可自定义)

//    self.navigationController.navigationBarHidden = true;
}

- (void)viewWillDisappear:(BOOL)animated {
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    [MobClick endLogPageView:@"价格筛选页"];

//    self.navigationController.navigationBarHidden = false;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - Configuring the view’s layout behavior
- (void)insertBackground {
    UIImageView *backgroundImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, Main_Screen_Width, Main_Screen_Height)];
    [backgroundImageView setImage:[UIImage imageNamed:@"screen_bg"]];
    [self.view insertSubview:backgroundImageView atIndex:0];
}

- (void)configureTableViews {
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.tableView setBounces:false];
    selectedRow = 0;
}

- (void)configureSliders {
    rangeSlider = [[NMRangeSlider alloc]initWithFrame:CGRectMake(0, 0, Main_Screen_Width - 24, 30)];
    [self.sliderView addSubview:rangeSlider];
    rangeSlider.minimumValue = 0;
    rangeSlider.maximumValue = 5000;
    rangeSlider.upperValue = 5000;
    rangeSlider.lowerValue = 0;
    [rangeSlider addTarget:self action:@selector(sliderValueChanged) forControlEvents:UIControlEventValueChanged];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    [cell setBackgroundColor:[UIColor clearColor]];
    [cell.textLabel setTextColor:[UIColor whiteColor]];
    [cell.textLabel setFont:[UIFont systemFontOfSize:15]];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    [cell setTintColor:[UIColor groupTableViewBackgroundColor]];
    NSInteger row = indexPath.row;
    switch (row) {
        case 0:
            [cell.textLabel setTextColor:RGBACOLOR(255, 118, 108, 1)];
            [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
            [cell.textLabel setText:@"全部"];
            break;
        case 1:
            [cell.textLabel setText:@"一室"];
            break;
        case 2:
            [cell.textLabel setText:@"二室"];
            break;
        case 3:
            [cell.textLabel setText:@"三室"];
            break;
        default:
            break;
    }
    return cell;
}

#pragma mark - Table view delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:selectedRow inSection:0]] setAccessoryType:UITableViewCellAccessoryNone];
    [[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:selectedRow inSection:0]].textLabel setTextColor:[UIColor whiteColor]];
    selectedRow = indexPath.row;
    [[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:selectedRow inSection:0]] setAccessoryType:UITableViewCellAccessoryCheckmark];
    [[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:selectedRow inSection:0]].textLabel setTextColor:RGBACOLOR(255, 118, 108, 1)];
}
#pragma mark - Triggering Methods
- (IBAction)clickToCancelBack:(UIButton *)sender {
    [self dismissViewControllerAnimated:true completion:^{
        
    }];
}

- (IBAction)clickToDoneFilter:(UIButton *)sender {
    NSString *count = [NSString stringWithFormat:@"%ld",(long)selectedRow];
    if (selectedRow == 0) {
        count = @"";
    }
    [self.delegate filterDoneWithHouseCount:count WithLowerPrice:rangeSlider.lowerValue UpperPrice:rangeSlider.upperValue];
    [self dismissViewControllerAnimated:true completion:^{
        
    }];
}

- (void)sliderValueChanged {
    [self.minRentLabel setText:[NSString stringWithFormat:@"%d",(int)rangeSlider.lowerValue]];
    [self.maxRentLabel setText:[NSString stringWithFormat:@"%d",(int)rangeSlider.upperValue]];
}
//- (void)sliderValueChanged:(UISlider *)sender {
//    if ([sender isEqual:self.minRentSlider]) {
//        if (sender.value >= self.maxRentSlider.value) {
//            sender.value = self.maxRentSlider.value;
//        }
//        [self.minRentLabel setText:[NSString stringWithFormat:@"%d",(int)sender.value]];
//    } else {
//        if (sender.value <= self.minRentSlider.value) {
//            sender.value = self.minRentSlider.value;
//        }
//        [self.maxRentLabel setText:[NSString stringWithFormat:@"%d",(int)sender.value]];
//    }
//}

@end
